package com.ugoodtech.mdcc.client.service.impl;

        /* Copyright (C) Ugoodtech, Inc - All Rights Reserved
         * Unauthorized copying of this file, via any medium is strictly prohibited
         * Proprietary and confidential
         */

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.client.service.StationService;
import com.ugoodtech.mdcc.client.service.UserLogService;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.repository.StationRepository;
import com.ugoodtech.mdcc.core.repository.UserLogRepository;
import com.ugoodtech.mdcc.core.repository.UserProfileRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Service
public class UserLogServiceImpl implements UserLogService {

    @Autowired
    private UserLogRepository userLogRepository;

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private StationRepository stationRepository;

    @Autowired
    private StationService stationService;

    @Override
    public UserLog addUserLog(User user, String stationNum, String operateType, String businessType, Long dataId, Map<String, Object> data) {
        UserLog userLog = null;
        try {
            userLog = new UserLog();
            userLog.setPlatform("微信端");
            userLog.setUser(user);
            userLog.setOperateType(operateType);
            userLog.setBusinessType(businessType);
            userLog.setUser(user);
            HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            userLog.setOperateIp(httpServletRequest.getRemoteAddr());
            if (null != user) {
                UserProfile userProfile = userProfileRepository.findByUserId(user.getId());
                userLog.setName(userProfile.getRealName());
            }
            if (StringUtils.isNotEmpty(stationNum)){
                try {
                    Station station=stationRepository.findByStationNum(stationNum);
                    userLog.setStationNo(station.getStationNum());
                    userLog.setStationName(station.getName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            userLog.setOperateDataId(dataId);
            if (null != data) {
                if (null != data.get("str")) {
                    String str=data.get("str") + "";
                    userLog.setOperateDesc(str.replace(",",",\r\n<br>"));
                }
            }
            userLogRepository.save(userLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userLog;
    }

    @Override
    public UserLog addUserLog(User user, String stationNum, Long reportId, String category, String operateType, String businessType, Long dataId, Map<String, Object> data) {
        UserLog userLog = null;
        try {
            userLog = new UserLog();
            userLog.setPlatform("微信端");
            userLog.setUser(user);
            userLog.setOperateType(operateType);
            userLog.setBusinessType(businessType);
            userLog.setUser(user);
            HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            userLog.setOperateIp(httpServletRequest.getRemoteAddr());
            if (null != user) {
                UserProfile userProfile = userProfileRepository.findByUserId(user.getId());
                userLog.setName(userProfile.getRealName());
            }

            if (StringUtils.isNotEmpty(stationNum)){
                try {
                    Station station=stationRepository.findByStationNum(stationNum);
                    userLog.setStationNo(station.getStationNum());
                    userLog.setStationName(station.getName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            userLog.setOperateDataId(dataId);
            if (null != data) {
                if (null != data.get("str")) {
                    String str=data.get("str") + "";
                    userLog.setOperateDesc(str.replace(",",",\r\n<br>"));
                }
            }
            if (reportId != null && reportId != 0) {
                userLog.setReportId(reportId);
            }
            if (StringUtils.isNotEmpty(category)) {
                userLog.setCategory(category);
            }
            userLogRepository.save(userLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userLog;
    }

    @Override
    public Page<UserLog> queryUserLogs(Pageable pageable) {
        QUserLog qUserLog = QUserLog.userLog;
        BooleanBuilder builder = new BooleanBuilder();
//        if (null != groupId) {
//            builder.and(qUserLog.qGroup.id.eq(groupId));
//        }
        Page<UserLog> userLogs = userLogRepository.findAll(builder, pageable);
        return userLogs;
    }
}
